package ru.vboxdc.impl.vbox;

import com.sun.xml.ws.commons.virtualbox.IMachine;
import com.sun.xml.ws.commons.virtualbox.IVirtualBox;
import com.sun.xml.ws.commons.virtualbox.IWebsessionManager;
import ru.vboxdc.domain.VirtualMachine;
import ru.vboxdc.iface.HostServerManager;
import ru.vboxdc.iface.VirtualMachineManager;
import ru.vboxdc.impl.vbox.VBoxConstant;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import org.apache.log4j.Logger;

/**
 * User: lelon
 * Date: 30.01.2009
 * Time: 12:09:58
 */
public class VBoxHostServerManager implements HostServerManager {
    private String version;
    /**
     *
     */
    private String operatingSystem;
    /**
     *
     */
    private Long memorySize;
    /**
     *
     */
    private Long processorCount;
    /**
     *
     */
    private Long memoryAvailable;
    /**
     * The IVirtualBox interface represents the main interface exposed by the product that
     * provides virtual machine management.
     */
    private IVirtualBox virtualBox;
    /**
     * Websession manager.
     * This provides essential services to webservice clients.
     */
    private IWebsessionManager websessionManager;
    /**
     * Logger
     */
    private static Logger logger = Logger.getLogger(VBoxHostServerManager.class);
    /**
     * Attribute <p>
     * Server parameters
     * In Key field - parameter name
     * In Value field- parameter value
     */
    private Map<String, String> parameters;
//    private VBoxHelper vBoxHelper;

    /**
     * Attribute <p>
     * State server connection.
     * true - Success  connection
     * false - Failed  connection
     */
    private Boolean stateServerConnection = false;

    /**
     * Public method
     * Set websessionManager and virtualBox
     *
     * @return state server connection - true or false
     */
    public boolean setConnection() {
//        websessionManager = new IWebsessionManager(getParameter(VBoxConstant.HOSTNAME_PARAMETER));
        try {
            virtualBox = websessionManager.logon(getParameter(VBoxConstant.USER_NAME_PARAMETER), getParameter(VBoxConstant.PASSWORD_PARAMETER));
            logger.debug("Initialized connection to VirtualBox version " + virtualBox.getVersion());
            setStateServerConnection(true);
            return true;
        } catch (Exception e) {
            logger.error(e);
            setStateServerConnection(false);
            return false;
        }
    }

    public VBoxHostServerManager(Map param) {
        parameters = new HashMap(param);
        websessionManager = new IWebsessionManager(getParameter(VBoxConstant.HOSTNAME_PARAMETER));
        if (setConnection()) {
            version = virtualBox.getVersion() + " " + virtualBox.getRevision();
            operatingSystem = virtualBox.getPackageType();
            memorySize = virtualBox.getHost().getMemorySize();
            processorCount = virtualBox.getHost().getProcessorCount();
        }
    }


    @Override
    public String getVersion() {
        return version;
    }

    @Override
    public String getOperatingSystem() {
        return operatingSystem;
    }

    @Override
    public Long getMemorySize() {
        return memorySize;
    }

    @Override
    public Long getProcessorCount() {
        return processorCount;
    }

    @Override
    public Long getMemoryAvailable() {
        memoryAvailable = virtualBox.getHost().getMemoryAvailable();
        return memoryAvailable;
    }

    @Override
    public List<VirtualMachine> getVirtualMachines() {

        List<VirtualMachine> listMachine = new ArrayList<VirtualMachine>();
        for (IMachine iMachine : virtualBox.getMachines2()) {

            VirtualMachine virtualMachine = new VirtualMachine();
            VirtualMachineManager virtualMachineManager = new VBoxVirtualMachineManager(iMachine, parameters);
            //TODO: не правильно что тут устанавливаются некоторые аттрибуты машинок??
            virtualMachineManager.setOSType(iMachine.getOSTypeId());
            virtualMachine.setManager(virtualMachineManager);
            virtualMachine.setName(iMachine.getName());
            virtualMachine.setId(virtualMachineManager.getLastId());
            listMachine.add(virtualMachine);
        }
        return listMachine;
    }

    public String getParameter(String parameterName) {
        return parameters.get(parameterName);
    }

    public Map<String, String> getParameters() {
        return parameters;
    }


    public void setStateServerConnection(Boolean stateServerConnection) {
        this.stateServerConnection = stateServerConnection;
    }

    public Boolean getStateServerConnection() {
        return stateServerConnection;
    }
}
